package com.example.demo.demos.web.controller;

import com.example.demo.demos.web.comment.response.JsonResult;
import com.example.demo.demos.web.comment.response.StatusCode;
import com.example.demo.demos.web.mapper.CommentMapper;
import com.example.demo.demos.web.pojo.dto.CommentDTO;
import com.example.demo.demos.web.pojo.entity.Comment;
import com.example.demo.demos.web.pojo.vo.CommentVO;
import com.example.demo.demos.web.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "03.评论模块")
@Slf4j
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @ApiOperation(value = "插入评论功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        log.debug("commentDTO = " + commentDTO);
        UserVO userVO= (UserVO) session.getAttribute("user");
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        commentMapper.insertComment(comment);
        return JsonResult.ok();
    }
    @ApiOperation(value = "根据微博id查询评论功能")
    @GetMapping("selectByWeiboId")
    public JsonResult selectByWeiboId(Long id){
        log.debug("id = " + id);
        List<CommentVO> commentVOS = commentMapper.selectByWeiboId(id);
        return JsonResult.ok(commentVOS);
    }
}
